package logic

import (
	"context"

	"server/service/user/internal/svc"
	"server/service/user/pb/user"

	"github.com/zeromicro/go-zero/core/logx"
)

type LoginByPasswordLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewLoginByPasswordLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginByPasswordLogic {
	return &LoginByPasswordLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *LoginByPasswordLogic) LoginByPassword(in *user.LoginReq) (*user.LoginResp, error) {
	_user, err := l.svcCtx.UserModel.LoginByPassword(l.ctx, in.Mobile, in.Password)

	if err != nil {
		logx.Errorf("LoginByPassword Mobile %s Password %s  error: %v", in.Mobile, in.Password, err)
		return nil, err
	}

	if _user == nil {
		return &user.LoginResp{}, nil
	}

	return &user.LoginResp{
		UserId:   int64(_user.Id),
		Username: _user.Username,
		Avatar:   _user.Avatar,
	}, nil
}
